package org.vs.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.vs.annotation.ImageField;
import org.vs.config.StaticResourceConfig;
import org.vs.handler.DataSettingHandler;
import org.vs.result.RestResult;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.Field;

@Component
@Aspect
public class ConcatenateStringAOP {
    @Resource
    private DataSettingHandler dataSettingHandler;

    @Pointcut("execution(* org.vs.controller..*.*(..))")
    public void pointcut() {
    }

    @Before(value = "pointcut()")
    public void before(JoinPoint joinPoint) throws IOException, IllegalAccessException {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            dataSettingHandler.traverseObject(arg, true);
        }
    }

    @AfterReturning(value = "pointcut()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) throws IOException, IllegalAccessException {
        String name = joinPoint.getSignature().getName();
        if (!(result instanceof RestResult)) {
            return;
        }
        RestResult restResult = (RestResult) result;
        dataSettingHandler.traverseObject(restResult, false);
    }
}
